package com.hymall.controller.wx;

import com.hymall.bean.MarketFootprint;
import com.hymall.bean.MarketUser;
import com.hymall.bean.bo.WxGoodsListBO;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.BaseRespVo;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.vo.WxGoodsCategoryVO;
import com.hymall.bean.vo.WxGoodsDetailVO;
import com.hymall.bean.vo.WxGoodsListVO;
import com.hymall.service.admin.GoodsService;
import com.hymall.service.wx.FootprintService;
import com.hymall.service.wx.WxGoodsService;
import com.hymall.service.wx.WxGoodsServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author: 章鹏
 * @description 小程序 → 商品模块
 * @date: 2022/7/19 17:15
 */
@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {
    @Autowired
    WxGoodsService wxGoodsService;

    /**
     * @author 章鹏
     * @description 获取上架商品数量
     * @date 2022/7/20 9:51
     * @param []
     * @return com.hymall.bean.common.BaseRespVo
     */
    @GetMapping("count")
    public BaseRespVo count() {
        int onSaleNum = wxGoodsService.getOnSaleNum();
        return BaseRespVo.ok(onSaleNum);
    }


    /**
     * @author 章鹏
     * @description 获取二级类别的父类类别下和所有的兄弟类别
     * @date 2022/7/19 22:49
     * @param [id]
     * @return com.hymall.bean.common.BaseRespVo
     */
    @RequestMapping("category")
    public BaseRespVo getCategories(Integer id) {
        WxGoodsCategoryVO categories = wxGoodsService.getCategories(id);
        return BaseRespVo.ok(categories);
    }

    /**
     * @author 章鹏
     * @description 加载商品主页面
     * @date 2022/7/20 9:50
     * @param [id]
     * @return com.hymall.bean.common.BaseRespVo
     */

    @Autowired
    FootprintService footprintService;
    @GetMapping("detail")
    public BaseRespVo detail(String id) {
        WxGoodsDetailVO goodsDetail = wxGoodsService.getGoodsDetail(Integer.parseInt(id));

        /**
         * @author 牟治宇
         * @description 添加历史足迹
         * @date 2022/7/20 19:58
         * @param [id]
         * @return com.hymall.bean.common.BaseRespVo
         */



        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (marketUser!=null){
            Integer userId = marketUser.getId();
            MarketFootprint marketFootprint = new MarketFootprint();
            marketFootprint.setId(null);
            marketFootprint.setUserId(userId);
            marketFootprint.setGoodsId(Integer.parseInt(id));
            marketFootprint.setAddTime(new Date());
            marketFootprint.setUpdateTime(marketFootprint.getAddTime());
            marketFootprint.setDeleted(false);
            footprintService.insertIntoFootprintList(marketFootprint);
        }

        return BaseRespVo.ok(goodsDetail);
    }

    /**
     * @author 章鹏
     * @description 商品详情 → 加载相关商品页面
     * @date 2022/7/20 10:06
     * @param [id]
     * @return com.hymall.bean.common.BaseRespVo
     */
    @GetMapping("related")
    public BaseRespVo related(String id) {
        CommonData relatedGoods = wxGoodsService.getRelatedGoods(Integer.parseInt(id));
        return BaseRespVo.ok(relatedGoods);
    }


    /**
     * @author 章鹏
     * @description 商品列表
     * @date 2022/7/20 11:14
     * @param [id]
     * @return com.hymall.bean.common.BaseRespVo
     */
    @GetMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo, WxGoodsListBO wxGoodsListBO) {
        WxGoodsListVO wxGoodsListVO = wxGoodsService.list(basePageInfo, wxGoodsListBO);
        wxGoodsService.addHistory(wxGoodsListBO);
        return BaseRespVo.ok(wxGoodsListVO);
    }




}
